package it.cast.zhang05.parallax;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

import it.cast.zhang05.domain.Cheeses;
import it.cast.zhang05.ui.parallaxDemo;

public class MainActivity extends AppCompatActivity {
    private parallaxDemo pld_add_header;
    private ImageView iv_header;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pld_add_header = (parallaxDemo) findViewById(R.id.pld_add_header);
        View headView = View.inflate(getApplicationContext(),R.layout.item_show,null);
        iv_header = (ImageView) headView.findViewById(R.id.iv_header);
        //添加一个头布局
        pld_add_header.addHeaderView(headView);
        //等View 的树状结构全部渲染完毕的时候，在设置到pld里面
        iv_header.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //宽 高已经测量完毕
                pld_add_header.setParallaxImage(iv_header);
                System.out.println(iv_header.getHeight());
                //把当前对象从这里View树里面移除掉，下次渲染的时候就不会调用了
                iv_header.getViewTreeObserver().removeGlobalOnLayoutListener(this);

            }
        });


        pld_add_header.setAdapter(new ArrayAdapter<>(this,android.R.layout.simple_list_item_1, Cheeses.NAMES));
    }
}
